/*  David Neimeyer 2007 |  www.eathaiku.com
 * -----------------------------------------------------------
 *
 * Mootools events for bookstore component (for use within Joomla 1.5)
 *  specifically for bookstore api use by calendar component(basically, don't show thumbs)
 *
 */

window.addEvent('domready', function(){
		var items = $$('.ann');
		items.each(function(item){
		
			item.addEvent('click', function(e) {
				var isbn = item.getAttribute('name');
				e = new Event(e).stop();		
				//alert(isbn.substr(0,6));		
				if(isbn.substr( 0,6) != 'detail'){
					var item_div = $('ann'+isbn);			
					var fx = item_div.effects({duration: 1000, transition: Fx.Transitions.Circ.easeInOut});
					
					//define image
					var box = $('box'+isbn);
					var fxbox =  box.effects({duration: 1200,wait:false, transition: Fx.Transitions.Circ.easeInOut});
					//var thumb = $('image'+isbn);
					var thumb = document.createElement('img');
					thumb.setAttribute('id', 'img' + isbn);
					//var thumb = $('img'+isbn);
					
					//alert(thumb);
					//var fx2 = thumb.effects({duration: 800,wait:true, transition: Fx.Transitions.Circ.easeInOut});
					
					//define p
					var p = document.createElement('p');
					p.setAttribute('id', 'p_ann' + isbn);
					var url = "/stable/components/com_bookstore/detail.php?isbn=" + isbn;
					var jSonRequest = new Json.Remote(url, {onComplete: function(details){
						//alert(details.annotation);
						var review = document.getElementById('rev_div'+isbn);
						if(review){
							var det_ann = document.createElement('div');
							det_ann.innerHTML = details.annotation;
						//	var det_ann = document.createTextNode(details.annotation);
							//var det_ann = details.annotation;
							var rev_ann = review.cloneNode(true);
							rev_ann.style.display = 'block';
							
							p.appendChild(rev_ann);
							p.appendChild(det_ann);
						//	alert(rev_ann);
						//	p.innerHTML += det_ann ;
						//	p.innerHTML += rev_ann;
							//alert(comb);
						}else{
							p.innerHTML = details.annotation;
						}
						//p.appendChild( document.createTextNode( details.annotation ) ); 

						// uncomment for isbn cover
						//	thumb.setAttribute('src',details.graphics);
						
					}}).send();
					
					
					//var myAjax = new Ajax(url, { method: 'get', update: p  }).request();
				//	var myAjax = new Ajax(url, { method: 'get', 
					//								OnSuccess: function(){
						//									alert(this.response.text);
							//						}
					//
						//								  }).request();
					function p_height(obj){
						//var len = obj.textContent.length;
						var len = p.innerHTML.length;
						if( len > 240){
							var remainder = len % 4;
							var height = ( (len - remainder)/4) ;
							return height;
						}else{
							var height = '68';
							return height;
						}
					}
					
					// uncomment for isbn cover
					/*
					fxbox.start({'width': '100px', 'height': '150px', 'opacity': '1'})
						.chain( function(){ 
							thumb.setAttribute('width',100);
							thumb.setAttribute('height',150);
							box.appendChild(thumb);
							//this.start({'opacity':'1','width': '100px','height': '150px'});
						});
					*/
					
					fx.start({'opacity' : 0}).
						chain(function(){ 
							//box.appendChild(thumb);
							//item.parentNode.	
							p.setAttribute('class','p_ann');					
							item_div.insertBefore(p, item_div.firstChild);
							//alert(p.innerHTML.length);
							h = p_height(p);
							//h =p.innerHTML.length;
							item.setHTML('Hide Details');
							item.setAttribute('class','xann');
							item.setAttribute('name','detail' + isbn);
							this.start({
											'width': '600px',
											'height': h+'px'
										});//end thisstart
						}).
						chain(function(){
							//fx2.start({'width': '100px','height': '150px',	'opacity': 1});
							this.start({'opacity' : 1});
						});//end chain/start
					}else{ //detail is already there
						//alert(isbn.substr(6));
						var box = $('box'+isbn.substr(6));
						var fxbox =  box.effects({duration: 800,wait:false, transition: Fx.Transitions.Circ.easeInOut});
						var thumb = $('img' + isbn.substr(6));
						// uncomment for isbn cover
						//	fxbox.start({'opacity':0}).chain(function(){ box.removeChild(thumb); this.start({'width': '0px','height': '0px'});});
						var item_div = $('ann'+isbn.substr(6));	
						var p = $('p_ann' + isbn.substr(6));			
						var fx = item_div.effects({duration: 1000, transition: Fx.Transitions.Circ.easeInOut});
						fx.start({'opacity' : '0'}).
						chain(function(){ 
							var html = 'Show Details';
							item.setHTML(html);
							item.setAttribute('class','ann');
							item.setAttribute('name',isbn.substr(6));
							item_div.removeChild(p);
							//p.removeChild(p.childNodes);
							this.start({
											'width': '100px',
											'height': '20px'
										});//end thisstart
						}).
						chain(function(){
							this.start({'opacity' : 1});
							
						});//end chain/start
					}//end if detailisbn
			});// event
		});//end each
		///////////////////////////////////// 
		/////////////////////////////////////
		
}); //end window event